package com.ctg.itrdc.sysmgr.portal.service.aop;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ctg.itrdc.sysmgr.portal.api.dto.FuncCompDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.FuncMenuDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.PrivFuncRelDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.PrivGrantDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.PrivilegeDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemPostDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemPostRoleDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemRolesDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserPostDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserRoleDTO;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanFuncCompCacheStrategy;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanFuncMenuCacheStrategy;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanPrivFuncRelCacheStrategy;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanPrivGrantCacheStrategy;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanPrivilegeCacheStrategy;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanSystemPostCacheStrategy;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanSystemPostRoleCacheStrategy;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanSystemRolesCacheStrategy;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanSystemUserCacheStrategy;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanSystemUserPostCacheStrategy;
import com.ctg.itrdc.sysmgr.portal.service.aop.strategy.cache.CleanSystemUserRoleCacheStrategy;

/**
 * @author blackfox 用作权限校验。 补充用户缓存，key为sysUserCode，其他使用dao的缓存处理
 * @param <T>
 */
//@Aspect
//@Component
@Deprecated
public class CleanCacheAOP {

	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Pointcut(value = // "execution(* insert(..))"+ 
			"execution(* updateByPrimaryKey(..))"
			+ "|| execution(* updateNotNullFieldsByPrimaryKey(..))"
			+ "|| execution(* deleteByPrimaryKey(..))"
			+ "|| execution(* batchDeleteByPrimaryKey(..))"
			+ "|| execution(* batchUpdateByPrimaryKey(..))")
	private void pointcut() {
	}

	/**
	 * 更新数据后，清理缓存 TODO 用户量大的时候，建议去除系统用户缓存
	 * 
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	@Before(value = "pointcut() && args(args)")
	public void clear(Object args) throws IllegalArgumentException,
			IllegalAccessException {

		if (args instanceof List) {
			List<?> dtoList = (List<?>) args;
			cleanListCache(dtoList);
		} else {
			cleanSingleCache(args);
		}
	}

	private void cleanSingleCache(Object args) {

		// 使用run而不是execute，是为了串行的方式删除缓存
		if (args instanceof SystemUserDTO) {
			new CleanSystemUserCacheStrategy(args).run();
		} else if (args instanceof SystemPostDTO) {
			new CleanSystemPostCacheStrategy(args).run();
		} else if (args instanceof SystemRolesDTO) {
			new CleanSystemRolesCacheStrategy(args).run();
		} else if (args instanceof SystemPostRoleDTO) {
			new CleanSystemPostRoleCacheStrategy(args).run();
		} else if (args instanceof SystemUserRoleDTO) {
			new CleanSystemUserRoleCacheStrategy(args).run();
		} else if (args instanceof PrivGrantDTO) {
			new CleanPrivGrantCacheStrategy(args).run();
		} else if (args instanceof PrivilegeDTO) {
			new CleanPrivilegeCacheStrategy(args).run();
		} else if (args instanceof PrivFuncRelDTO) {
			new CleanPrivFuncRelCacheStrategy(args).run();
		} else if (args instanceof FuncMenuDTO) {
			new CleanFuncMenuCacheStrategy(args).run();
		} else if (args instanceof FuncCompDTO) {
			new CleanFuncCompCacheStrategy(args).run();
		} else if (args instanceof SystemUserPostDTO) {
			new CleanSystemUserPostCacheStrategy(args).run();
		}
	}

	private void cleanListCache(List<?> dtoList) {
		
		if(CollectionUtils.isEmpty(dtoList)) return;

		// 使用run而不是execute，是为了以串行的方式删除缓存
		Object dto = dtoList.get(0);
		if (dto instanceof SystemUserDTO) {
			for (Object object : dtoList)
				new CleanSystemUserCacheStrategy(object).run();

		} else if (dto instanceof SystemPostDTO) {
			for (Object object : dtoList)
				new CleanSystemPostCacheStrategy(object).run();

		} else if (dto instanceof SystemRolesDTO) {
			for (Object object : dtoList)
				new CleanSystemRolesCacheStrategy(object).run();

		} else if (dto instanceof SystemPostRoleDTO) {
			for (Object object : dtoList)
				new CleanSystemPostRoleCacheStrategy(object).run();

		} else if (dto instanceof SystemUserRoleDTO) {
			for (Object object : dtoList)
				new CleanSystemUserRoleCacheStrategy(object).run();

		} else if (dto instanceof PrivGrantDTO) {
			for (Object object : dtoList)
				new CleanPrivGrantCacheStrategy(object).run();

		} else if (dto instanceof PrivilegeDTO) {
			for (Object object : dtoList)
				new CleanPrivilegeCacheStrategy(object).run();

		} else if (dto instanceof PrivFuncRelDTO) {
			for (Object object : dtoList)
				new CleanPrivFuncRelCacheStrategy(object).run();

		} else if (dto instanceof FuncMenuDTO) {
			for (Object object : dtoList)
				new CleanFuncMenuCacheStrategy(object).run();

		} else if (dto instanceof FuncCompDTO) {
			for (Object object : dtoList)
				new CleanFuncCompCacheStrategy(object).run();

		} else if (dto instanceof SystemUserPostDTO) {
			for (Object object : dtoList)
				new CleanSystemUserPostCacheStrategy(object).run();

		}
	}

}
